<?php
class Del_View_Helper_Horarios 
	extends Zend_View_Helper_Abstract
{
	

	private $_data = array();
	private $_values = array();
	private $_list = array();
	
	public function horarios( $horarios )
	{
		$this->_init();			
		$this->_setHorarios($horarios);			
		$this->_make($horarios);	
		return $this->_toHtml();		
	}
	
	
	protected function _init(){
		
		$hours	= $this->_getHours();
		$days	= $this->_getDays();
		
		$this->_data = $hours;
		
		foreach ($days as $day){
			
			$this->_values[$day] = array();
			
			foreach ($hours as $hour){
				$this->_values[$day][]= 0;
			}
		}
	}
	
	protected function _setHorarios ($horarios){
		
		foreach ($horarios as $horario){
			
			$day  = $horario->dia;
			$key1 = array_search( $horario->desde, $this->_data);
			$key2 = array_search( $horario->hasta, $this->_data);
			
			for ($i =$key1; $i<$key2; $i++){
				$this->_values[$day][$i] = 1;
			}
			
		}
	}
	
	protected function _make (){
		
		
		$hoursList	= $this->_getHours();
		
		foreach ($this->_values as $day=>$hours){
			
			foreach ($hours as $hour=>$val){
			
				$hourKey = $hoursList[$hour];
				
				$this->_list[$hourKey][$day]= $val;
			}
		}
		
	}
	
	
	
	protected function _toHtml (){
	
		$HTML = '<table class="horarios"><thead><tr><th class="col-hora"></th>';
		
		foreach ($this->_getDays() as $day){
			$HTML.="<th>{$day}</th>";
		}
		
		$HTML.="</tr></thead><tbody>";
		
		$n = 0;
		
		array_pop($this->_list);
		
		foreach ($this->_list as $hour=>$days){
			
			
			if ($n%2!=0){
				
				$HTML.= "<tr class='odd'>";
				
			} else {
				
				$HTML.= "<tr><th class='col-hora' rowspan='2'>".substr($hour, 0, 5)."</th>";
			}
			
			$n++;
			
//			$HTML.= "<tr{$class}><th class='col-hora'>".substr($hour, 0, 5)."</th>";
			
			foreach ($days as $day=>$val){
				
				if ($val){
					$HTML.= "<td class='open'></td>";
				} else {
					$HTML.= "<td></td>";
				}
						
			}
			$HTML.= "</tr>";
		}
		
		$HTML.="</tbody></table>";
		
		return $HTML;
		
	}
	
	protected function _getHours()
	{
		return array(	'00:00:00','00:30:00','01:00:00','01:30:00','02:00:00','02:30:00',
						'03:00:00','03:30:00','04:00:00','04:30:00','05:00:00','05:30:00',
						'06:00:00','06:30:00','07:00:00','07:30:00','08:00:00','08:30:00',
						'09:00:00','09:30:00','10:00:00','10:30:00','11:00:00','11:30:00',
						'12:00:00','12:30:00','13:00:00','13:30:00','14:00:00','14:30:00',
						'15:00:00','15:30:00','16:00:00','16:30:00','17:00:00','17:30:00',
						'18:00:00','18:30:00','19:00:00','19:30:00','20:00:00','20:30:00',
						'21:00:00','21:30:00','22:00:00','22:30:00','23:00:00','23:30:00', '24:00:00');	
		
		return array_combine($data, $data);
	}
	
	protected function _getDays()
	{
		return range( 1, 7);
	}
	
	
	
	
	
}